package com.yuedou.api.controller.product;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuedou.api.constant.ResponseCode;
import com.yuedou.api.controller.platform.BaseController;
import com.yuedou.api.entity.product.*;
import com.yuedou.api.service.product.*;
import com.yuedou.api.util.GsonResultBean;
import com.yuedou.api.util.Tools;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Author: ZhouWei
 *@Description:  商品分类相关接口
 *@Date 2025-01-17
 */
@Component
@RestController
@RequestMapping("/product/type")//url:模块/资源/{}/细分
public class ProductTypeController extends BaseController {
    @Resource
    private ProductTypeService productTypeService;
    @Resource
    private ProductService productService;

    @Value("${web.imgPrefix}")
    private String imgPrefix;//微信支付的回调地址

    /**
     * 获取所有分类
     * @param
     * @throws Exception
     */
    @PostMapping(value="/list")
    public JSONObject indexPage(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int parent_id = jsonRequest.containsKey("parent_id") ? jsonRequest.getInt("parent_id") : 0;
            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex") : 1;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize") : 10;
            String platform = jsonRequest.containsKey("platform") ? jsonRequest.getStr("platform") : "wxmini";

            double longitude = jsonRequest.containsKey("longitude") ? jsonRequest.getDouble("longitude") : 0;
            double latitude = jsonRequest.containsKey("latitude") ? jsonRequest.getDouble("latitude") : 0;
            Map<String,Object> map = new HashMap<String, Object>();
            //1.先获取父节点
            QueryWrapper<ProductType> qw = new QueryWrapper<>();
            qw.eq("state",0);
            if(platform.equals("wxmini")){
                qw.eq("is_show",1);
            }else if(platform.equals("dymini")){
                qw.eq("is_show_dy",1);
            }
            qw.eq("parent_id",parent_id);
            List<ProductType> list = productTypeService.list(qw);
            if(CollUtil.isNotEmpty(list)){
                for (ProductType type : list){
                    type.setImgUrl( imgPrefix + type.getImgUrl());
                }
            }
            if(list.size() > 0){
                List<Product> products = productService.queryProductList("",list.get(0).getId(),0,1,0,platform,pageIndex,pageSize);
                map.put("products",products);
            }
            map.put("types",list);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",list.size(), map),false,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }

    /**
     * 获取当前商家的所有分类
     * @param
     * @throws Exception
     */
    @PostMapping(value="/product/list/bg")
    public JSONObject productListByTypeBg(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int user_id = jsonRequest.containsKey("user_id") ? jsonRequest.getInt("user_id") : 0;
            int cateId = jsonRequest.containsKey("cateId") ? jsonRequest.getInt("cateId") : 0;

            int pageIndex = jsonRequest.containsKey("pageIndex") ? jsonRequest.getInt("pageIndex") : 1;
            int pageSize = jsonRequest.containsKey("pageSize") ? jsonRequest.getInt("pageSize") : 10;

            String keyword = jsonRequest.containsKey("keyword") ? jsonRequest.getStr("keyword") : null;
            String platform = jsonRequest.containsKey("platform") ? jsonRequest.getStr("platform") : "wxmini";

            Map<String,Object> map = new HashMap<>();

            //只搜索 商品
            //String productName,Integer isHot, int cateChildId, String platform,int pageIndex, int pageSize
            List<Product> products = productService.queryProductList(keyword, null, 1, cateId, platform,pageIndex,pageSize);
            map.put("products",products);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",products.size(), map),false,false);

        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }

    /** 获取所有分类
     * @param
     * @throws Exception
     */
    @PostMapping(value="/childList")
    public JSONObject childList(@RequestBody String requestParam) throws Exception{
        JSONObject result = null;
        try{
            if(StrUtil.isEmpty(requestParam)){
                return Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"参数异常"),true,true);
            }
            JSONObject jsonRequest = JSONUtil.parseObj(requestParam);
            int parent_id = jsonRequest.containsKey("parent_id") ? jsonRequest.getInt("parent_id") : 0;
            Map<String,Object> map = new HashMap<>();
            //1.先获取父节点
            List<String> names = new ArrayList<>();
            List<ProductType> types = productTypeService.selectListByColumn("parent_id",parent_id);
            map.put("types",types);
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.OK,"成功",types.size(), map),false,false);
        }catch (Exception e){
            result = Tools.getJson(new GsonResultBean<>(ResponseCode.FAIL,"系统异常",e.getMessage()),true,true);
            logger.error(e.toString(), e);
        }
        return result;
    }
}
